package com.itHuangTao.tank.collisions;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.itHuangTao.tank.GameType;
import com.itHuangTao.tank.ItemType;
import com.itHuangTao.tank.TankApp;
import com.itHuangTao.tank.components.TankLevelComponent;
import com.itHuangTao.tank.effects.HelmetEffect;
import com.itHuangTao.tank.effects.ShipEffect;

public class ItemPlayerHandler extends CollisionHandler {

    public ItemPlayerHandler() {
        super(GameType.ITEM, GameType.PLAYER);
    }

    @Override
    protected void onCollisionBegin(Entity item, Entity player) {
        //获得道具的类型
        ItemType itemType = item.getObject("itemType");

        TankApp.PropCount--;
        //对道具的类型进行选择判断
        switch (itemType) {
            //星星：坦克的等级加一
            case STAR -> player.getComponent(TankLevelComponent.class).upGrade();
            //枪：坦克的等级直接满级
            case GUN -> player.getComponent(TankLevelComponent.class).restoreFully();
            //坦克，玩家生命值加一
            case TANK -> player.getComponent(HealthIntComponent.class).restore(1);
            //爱心，玩家生命值加满
            case HEART -> player.getComponent(HealthIntComponent.class).restoreFully();
            //炸弹，使得敌方出现的坦克全部原地爆炸
            case BOMB -> {
                FXGL.getGameWorld()
                        .getEntitiesByType(GameType.ENEMY)
                        .forEach(entity -> {
                            FXGL.spawn("explode", entity.getCenter()
                                    .subtract(50 / 2.0, 50 / 2.0));
                            entity.removeFromWorld();
                            FXGL.inc("destroyedEnemyAmount", 1);
                        });
            }
            //轮船道具，可以使得坦克在水面上行走
            case SHIP -> {
                player.getComponent(EffectComponent.class).startEffect(new ShipEffect());
            }
            //时间，可以使得敌方坦克静止5秒钟
            case TIME -> {
                FXGL.<TankApp>getAppCast().freezeEnemy();
            }
            //钢盔，可以使得玩家无敌10秒
            case HELMET -> {
                player.getComponent(EffectComponent.class).startEffect(new HelmetEffect());
            }
            //铲子，可以升级基地的保护墙
            case SPADE -> {
                FXGL.<TankApp>getAppCast().reinforce();
            }
        }
        //移除道具
        item.removeFromWorld();
        //播放拾取道具的声音
        FXGL.play("item.wav");

    }
}
